package com.rati.usercenter.controller;


import com.rati.usercenter.common.BaseResponse;
import com.rati.usercenter.exception.BusinessException;
import com.rati.usercenter.model.DTO.TeamInfoDTO;
import com.rati.usercenter.model.VO.TeamVO;
import com.rati.usercenter.model.VO.UserVO;
import com.rati.usercenter.model.domain.Team;
import com.rati.usercenter.model.request.JoinTeamRequest;
import com.rati.usercenter.model.request.TeamQuery;
import com.rati.usercenter.service.TeamService;
import com.rati.usercenter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.rati.usercenter.common.ErrorCode.*;
import static com.rati.usercenter.utils.ResultUtils.*;


/**
 * 用户接口
 *
 * @author Lwh
 * @time 2022/12/11 20:41
 */

@RestController
@RequestMapping("/team")
@Slf4j
@CrossOrigin(origins = {"http://localhost:3000"})
public class TeamController {

    @Resource
    private UserService userService;

    @Resource
    private TeamService teamService;

    /**
     * 创建新队伍
     *
     * @param teamInfoDTO 队伍信息
     * @return 新队伍id
     */
    @PostMapping("/create")
    public BaseResponse<Long> createTeam(@RequestBody TeamInfoDTO teamInfoDTO, HttpServletRequest request) {
        if (teamInfoDTO == null) {
            throw new BusinessException(PARAMS_ERROR, "请求参数为空");
        }
        UserVO currentUser = userService.getCurrentUser(request);
//        数据转换
        Team team = new Team();
        teamService.copyAndSetTime(teamInfoDTO, team);
        team.setCreateUserId(currentUser.getId());
        long result = teamService.createTeam(team, currentUser);
        return success(result);
    }

    /**
     * 根据id删除队伍（解散）
     *
     * @param id 要删除的队伍id
     * @return 是否成功
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(@RequestParam long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(PARAMS_ERROR, "请求参数为空");
        }
        UserVO currentUser = userService.getCurrentUser(request);
        teamService.deleteTeam(id, currentUser);
        return success(true);
    }

    /**
     * 根据队伍id修改队伍信息
     *
     * @param teamInfoDTO 修改后的队伍信息
     * @return 是否成功
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamInfoDTO teamInfoDTO, HttpServletRequest request) {
        if (teamInfoDTO == null) {
            throw new BusinessException(PARAMS_ERROR, "请求参数为空");
        }
        UserVO currentUser = userService.getCurrentUser(request);
        teamService.updateTeam(teamInfoDTO, currentUser);
        return success(true);
    }

    /**
     * 根据id查询队伍
     *
     * @param id 队伍id
     * @return 队伍信息
     */
    @GetMapping("/get")
    public BaseResponse<Team> getById(long id) {
        if (id <= 0) {
            throw new BusinessException(PARAMS_ERROR, "请求参数不合法");
        }
        Team team = teamService.getById(id);
        if (team == null) {
            throw new BusinessException(NULL_ERROR, "无符合条件的队伍");
        }
        return success(team);
    }

    /**
     * 获取用户创建的队伍
     *
     * @return 用户创建的队伍列表
     */
    @GetMapping("/get/create")
    public BaseResponse<List<TeamVO>> getCreateTeam(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(PARAMS_ERROR, "请求参数未空");
        }
        UserVO currentUser = userService.getCurrentUser(request);
        Long userId = currentUser.getId();
        List<TeamVO> teamVOList = teamService.getCreateTeam(userId);
        if (teamVOList.isEmpty()) {
            throw new BusinessException(NULL_ERROR, "该用户未创建队伍");
        }
        return success(teamVOList);
    }

    /**
     * 获取用户加入的队伍
     *
     * @return 用户加入的队伍列表
     */
    @GetMapping("/get/joined")
    public BaseResponse<List<TeamVO>> getJoinedTeam(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(PARAMS_ERROR, "请求参数未空");
        }
        UserVO currentUser = userService.getCurrentUser(request);
        Long userId = currentUser.getId();
        List<TeamVO> teamVOList = teamService.getJoinedTeam(userId);
        if (teamVOList.isEmpty()) {
            throw new BusinessException(NULL_ERROR, "该用户未加入队伍");
        }
        return success(teamVOList);
    }

    /**
     * 根据条件查询队伍（可分页查询）
     *
     * @param teamQuery 队伍要符合的条件
     * @return 队伍列表
     */
    @GetMapping("/list")
    public BaseResponse<List<TeamVO>> getTeamList(@RequestBody TeamQuery teamQuery, HttpServletRequest request) {
        if (teamQuery == null) {
            throw new BusinessException(PARAMS_ERROR, "请求参数为空");
        }
        UserVO currentUser = userService.getCurrentUser(request);
        List<TeamVO> list = teamService.listTeam(teamQuery, currentUser);
        if (list.isEmpty()) {
            throw new BusinessException(NULL_ERROR, "无符合条件的队伍");
        }
        return success(list);
    }

    /**
     * 用户通过id加入队伍
     *
     * @param joinTeamRequest 加入申请类（队伍id和密码）
     * @return 是否成功
     */
    @PostMapping("/joinById")
    public BaseResponse<Boolean> joinTeam(@RequestBody JoinTeamRequest joinTeamRequest, HttpServletRequest request) {
        if (joinTeamRequest == null) {
            throw new BusinessException(PARAMS_ERROR, "请求参数为空");
        }
        long teamId = joinTeamRequest.getId();
        if (teamId <= 1) {
            throw new BusinessException(PARAMS_ERROR, "请求参数非法");
        }
        UserVO currentUser = userService.getCurrentUser(request);
        String password = joinTeamRequest.getPassword();
        teamService.joinTeamById(teamId, password, currentUser);
        return success(true);
    }

    /**
     * 用户退出队伍
     *
     * @param teamId 队伍id
     * @return 是否成功
     */
    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeam(@RequestParam long teamId, HttpServletRequest request) {
        if (teamId <= 1) {
            throw new BusinessException(PARAMS_ERROR, "请求参数非法");
        }
        UserVO currentUser = userService.getCurrentUser(request);
        teamService.quitTeam(teamId, currentUser);
        return success(true);
    }


}
